//---------------------------------------------------------------------
// Authors: jachymko
//
// Description: Class implementing the New-Shortcut command.
//
// Creation Date: Dec 13, 2006
//
//---------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.IO;
using System.Management.Automation;
using System.Runtime.InteropServices;

namespace Pscx.Commands.FileSystem
{
    using Interop;

    [Cmdlet(VerbsCommon.New, PscxNouns.Shortcut)]
    [Description("Creates shell shortcuts.")]
    public partial class NewShortcutCommand : NewLinkCommandBase
    {
        protected override void ProcessRecord()
        {
            if (FindFileOrDirectory(FullTarget) == null)
            {
                // The target file or directory doesn't exist.
                WriteError(PscxErrorRecord.FileNotFoundError(Target));
                return;
            }

            try
            {
                string lnkFile = System.IO.Path.ChangeExtension(FullPath, ".lnk");

                IShellLink link = (IShellLink)new CShellLink();
                link.SetPath(FullTarget);

                IPersistFile persist = (IPersistFile)(link);
                persist.Save(lnkFile, false);

                WriteObject(new FileInfo(lnkFile));
            }
            catch (Exception exc)
            {
                WriteError(new ErrorRecord(exc, "CreateShellLinkError", ErrorCategory.NotSpecified, Path));
            }
        }
    }
}
